<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/16
 * Time: 5:37 PM
 */

namespace App\Utils;

use App\BO\PipelineConfig;
use Illuminate\Pipeline\Pipeline;

class PipelineProxy
{
    /**
     * @var Pipeline
     */
    private static $pipeline;

    private static function getInstance()
    {
        if (!(self::$pipeline instanceof Pipeline)) {
            self::$pipeline = new Pipeline(app());
        }
        return self::$pipeline;
    }

    /**
     * 执行
     * @param mixed $data
     * @param PipelineConfig $config
     * @return bool
     */
    public static function execute($data, PipelineConfig $config)
    {
        return self::getInstance()
            ->via($config->getHandle())
            ->send($data)
            ->through($config->getPipes())
            ->then($config->getClosure());
    }
}
